// const htmlMinifier = require('html-minifier');

// 压缩选项
const minifyOptions = {
  collapseWhitespace: true,  // 压缩空白字符
  removeComments: true,      // 删除注释
  removeAttributeQuotes: true // 删除属性值的引号
};

// 要压缩的 HTML 字符串
const htmlString = `
  <div>
    <img src="image1.jpg" alt="Image 1">
    <img src="image2.jpg" alt="Image 2">
    <img src="image3.jpg" alt="Image 4">
  </div>
`;

// 使用正则表达式找到所有的 img 标签
const imgRegExp = /<img\s+[^>]*src="([^"]+)"[^>]*>/g;
const imgTags = htmlString.match(imgRegExp);

// 对每个 img 标签进行压缩
const compressedImgTags = imgTags.map((imgTag) => {
  const srcRegExp = /src="([^"]+)"/;
  const srcMatch = imgTag.match(srcRegExp);
  if (srcMatch) {
    const src = srcMatch[1];
    const compressedSrc = compressImage(src); // 压缩图片
    if (compressedSrc !== src) {
      imgTag = imgTag.replace(srcRegExp, `src="${compressedSrc}"`);
    }
  }
  return imgTag;
});

// 将压缩后的 img 标签拼接回原来的 HTML 字符串中
let compressedHtmlString = htmlString;
compressedImgTags.forEach((compressedImgTag, index) => {
  compressedHtmlString = compressedHtmlString.replace(imgTags[index], compressedImgTag);
});


function compressImage(src) {
  // 压缩图片并返回压缩后的图片路径
  return src
}

// 压缩整个 HTML 字符串
console.log('compressedHtmlString: ', compressedHtmlString);
// const finalHtmlString = htmlMinifier.minify(compressedHtmlString, minifyOptions);
